-- UIChessTable
-- Created by cheny3 Jul/26/2016
-- 棋桌

UIChessTable = class("UIChessTable", function()
    return cc.Layer:create();
end);

function UIChessTable.create(gridData)
    return UIChessTable.new(gridData);
end

-- 构造函数
function UIChessTable:ctor(gridData)
    self:setName("UIChessTable");

    -- 复用座钟界面
    local node = cc.CSLoader:createNode("layout/dungeon/special/DeskClock.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.gridData  = gridData;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIChessTable:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIChessTable:registerEventCallback()
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIChessTable 界面析构清理");
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIChessTable:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIChessTable");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 按钮回调
    local btnNode = findChildByName(node, "CT/btn_node");
    local btn1 = btnNode:getChildByName("btn_hour");
    local btn2 = btnNode:getChildByName("btn_minute");
    local btn3 = btnNode:getChildByName("btn_open");

    -- 选项
    local btns = {btn1, btn2, btn3};
    local options = self.gridData.options;
    for i = 1, 3 do
        self:registerBtn(btns[i], options[i]);
    end
end

-- 注册选项按钮
function UIChessTable:registerBtn(btn, optionId)
    -- 按钮文本
    local btnText = ChessTableM.query(optionId, "name");
    -- 注册按钮点击事件
    local function onbtnCClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            DungeonActionM.go("chess_table", self.gridData:getPos(), optionId);
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIChessTable");
        end
    end
    AddTouchEventListener(btn, onbtnCClick);
    TextStyleM.setTextStyle(btn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btn:setTitleText(btnText);
end

-- 重绘
function UIChessTable:redraw()
    -- 查询建筑配置
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");

    titleLabel:setString(buildingInfo["title"]);
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    iconImg:loadTexture(iconPath);
    local iconSize = iconImg:getContentSize();

    iconImg:setScale(1.3);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    self.dialog = buildingInfo["dialog"];

    applyString(descLabel, self.dialog);
end